package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Share;
import com.dotstone.chipism.util.MsgCode;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ShareAdapter extends BaseAdapter {
	private Context context;
	private List<Share> data;
	private Handler mHandler;

	public ShareAdapter(Context context) {
		this.context = context;
	}

	public ShareAdapter(Context context, List<Share> data) {
		this.data = data;
		this.context = context;
	}

	public void setHandler(Handler mHandler) {
		this.mHandler = mHandler;
	}

	public void setData(List<Share> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void clear() {
		this.data.clear();
		notifyDataSetChanged();
	}

	public void addAll(List<Share> data) {
		if (data != null) {
			this.data.addAll(data);
			notifyDataSetChanged();
		}
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.item_share, null);
			holdView = new HoldView();
			holdView.time = (TextView) view.findViewById(R.id.time_tv);
			holdView.name = (TextView) view.findViewById(R.id.name_tv);
			holdView.option = (TextView) view.findViewById(R.id.option_tv);
			view.setTag(holdView);
		} else {
			holdView = (HoldView) view.getTag();
		}
		Share s = data.get(position);
		holdView.time.setText(s.getTime());
		holdView.name.setText(s.getShareRemarks());
		holdView.option.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mHandler!=null){
					Message msg = Message.obtain();
					msg.what = MsgCode.CODE_REMOVE_SHARE;
					msg.arg1 = position;
					mHandler.sendMessage(msg);
				}
			}
		});
		return view;
	}

	HoldView holdView;

	class HoldView {
		private TextView time, name, option;
	}

}
